//
//  CNWelcomeViewController.swift
//  Ccweibo
//
//  Created by 陈锦聪 on 16/3/24.
//  Copyright © 2016年 Coco. All rights reserved.
//

import UIKit
import SDWebImage

class CNWelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        prepareUI()
        
        // 发送网络请求前先加载本地缓存图片
        if let string = CNUserAccountViewModel.sharedInstance.userAccount?.avatar_large {
            let url = NSURL(string: string)
            self.iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
        }
        
        CNUserAccountViewModel.sharedInstance.loadUserInfo { (error) -> () in
            if error != nil {
                CNPrint(item: "请求用户信息出错:\(error)")
                return
            }
            if let string = CNUserAccountViewModel.sharedInstance.userAccount?.avatar_large {
                let url = NSURL(string: string)
                self.iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
            }
        }
    }
    // 头像底部约束
//    var iconViewBottomCons: NSLayoutConstraint?
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        animation()
    }
    
    private func animation() {
        
        iconView.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(view).offset(-(UIScreen.mainScreen().bounds.height - 160))
        }
        
        UIView.animateWithDuration(0.9, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            // 执行layoutIfNeeded才有动画效果
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(defaultDuration, animations: { () -> Void in
                    self.welcomeLabel.alpha = 1
                    }, completion: { (_) -> Void in
//                        print("跳转界面")
                        AppDelegate.loadViewController(CNMainViewController())
                })
        }
    }
    
    private func prepareUI() {
        
        view.addSubview(bgImageView)
        view.addSubview(iconView)
        view.addSubview(welcomeLabel)
        
        bgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(UIEdgeInsetsZero)
        }
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.bottom.equalTo(view).offset(-160)
            make.size.equalTo(CGSize(width: 85, height: 85))
        }
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
    }
    // MARK: - lazyLoad
    private lazy var bgImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    private lazy var iconView: UIImageView = {
        
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        imageView.layer.cornerRadius = 42.5
        imageView.layer.masksToBounds = true
        return imageView
    }()
    private lazy var welcomeLabel: UILabel = {
        
        let label = UILabel()
        label.text = "欢迎归来"
        label.font = UIFont.systemFontOfSize(14)
        label.sizeToFit()
        label.alpha = 0
        return label
    }()
}
